package com.easywan.vo.tools.hearthstone;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.easywan.util.ApiUtils;
import com.easywan.util.ImageUtils;
import com.easywan.vo.Hearthstone;

public class HearthStoneCar extends Hearthstone{
	private Integer carType;//卡牌类型
	private Integer carPerfission;//卡牌职业
	private Integer carDegree; //卡牌品质
	private Integer magic;//卡牌消耗
	private String name;
	private String desc;//卡牌说明
	private String race;
	private Integer attach;//攻击
	private Integer health;//生命
	/**
	 * 原画地址：
	 * 	1.魔兽原画
	 * 		http://us.battle.net/wow/en/media/artwork/trading-card-game/
	 * 	2.魔兽原画2
	 * 		http://wow.blizzgame.ru/gallery/
	 */
	private String pic;//原画地址
	public Integer getCarType() {
		return carType;
	}
	public void setCarType(Integer carType) {
		this.carType = carType;
	}
	public Integer getCarPerfission() {
		return carPerfission;
	}
	public void setCarPerfission(Integer carPerfission) {
		this.carPerfission = carPerfission;
	}
	public Integer getCarDegree() {
		return carDegree;
	}
	public void setCarDegree(Integer carDegree) {
		this.carDegree = carDegree;
	}
	public void setMagic(Integer magic){
		this.magic = magic;
	}
	
	public String getCarFrame(){
		super.setCarType(this.carType);
		return super.getCarFrame(carPerfission);
	}
	/**
	 * 获取主架构图
	 * @return
	 */
	public BufferedImage getCarFrameImage(){
		String carFramePath = getCarFrame();
		try {
			return ImageUtils.getImage(carFramePath, ApiUtils.getHearthstoneImageApi(carFramePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getMagic(){
		super.setCarType(this.carType);
		return super.getMagic(magic);
	}
	/**
	 * 获取法力水晶
	 * @return
	 */
	public BufferedImage getMagicImage(){
		String magic = getMagic();
		try {
			return ImageUtils.getImage(magic, ApiUtils.getHearthstoneImageApi(magic));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取卡牌品质
	 * @return
	 */
	public BufferedImage getPowerImage(){
		String power = getPower();
		try{
			return ImageUtils.getImage(power, ApiUtils.getHearthstoneImageApi(power));
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public String getPower(){
		super.setCarType(this.carType);
		return super.getCarType(carDegree);
	}
	public BufferedImage drawName(BufferedImage bi){
		bi = ImageUtils.drawImg(bi,name,new Font("经典隶变简", Font.BOLD, 30),Color.white,375);
		return bi;
	}
	public BufferedImage drawDesc(BufferedImage bi){
		bi = ImageUtils.drawImg(bi,desc,new Font("微软雅黑", Font.PLAIN, 18),Color.black,480);
		return bi;
	}
	public BufferedImage drawRaceDesc(BufferedImage bi){
		bi = ImageUtils.drawImg(bi,race,new Font("经典隶变简", Font.BOLD, 20),Color.white,625);
		return bi;
	}
	/*public BufferedImage getRace(){
		BufferedImage race = ImageUtils.getImage(super.getRace(), ApiUtils.getHearthstoneImageApi(super.getRace()));
		return race;
	}*/
	public BufferedImage drawRace(BufferedImage bi){
		try{
			BufferedImage race = ImageUtils.getImage(super.getRace(), ApiUtils.getHearthstoneImageApi(super.getRace()));
			bi = ImageUtils.addImage(bi, race);
			//调用画种族名字
		}catch(Exception e){
			e.printStackTrace();
		}
		return bi;
	}
	public String getPic() {
		return pic;
	}
	public void setPic(String pic) {
		this.pic = pic;
	}
	public String getAttach(){
		super.setCarType(this.carType);
		return super.getHit(attach);
	}
	public BufferedImage getAttachImage(){
		String attath = getAttach();
		try{
			return ImageUtils.getImage(attath, ApiUtils.getHearthstoneImageApi(attath));
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public String getHealth(){
		super.setCarType(this.carType);
		return super.getHealth(health);
	}
	public BufferedImage getHealthImage(){
		String health = getHealth();
		try{
			return ImageUtils.getImage(health, ApiUtils.getHearthstoneImageApi(health));
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	private BufferedImage createCommonCar(){
		//绘制主架构
		BufferedImage bi = this.getCarFrameImage();
		//绘制插图
		BufferedImage picture = getPicture();
		picture = ImageUtils.addImageBack(getBg(),picture,80,40);
		//bi = ImageUtils.addImageBack(bi, picture,80,40);
		bi = ImageUtils.addImage( picture,bi);
		
		//绘制法力水晶
		bi = ImageUtils.addImage(bi, getMagicImage());
		//绘制品质
		bi = ImageUtils.addImage(bi, getPowerImage());
		//绘制名字
		bi = this.drawName(bi);
		//绘制说明
		bi = this.drawDesc(bi);
		return bi;
	}
	/**
	 * 绘制随从卡 或者 武器卡
	 * @return
	 */
	public BufferedImage createSuicongOrWuqiCar(){
		//绘制主架构
		BufferedImage bi = this.createCommonCar();
		//绘制种族
		if(race != null && race != "" && carType == 1){
			bi = this.drawRace(bi);
			//绘制种族说明
			bi = this.drawRaceDesc(bi);
		}
		if(super.getCarType() != 2){
			//绘制攻击力
			bi = ImageUtils.addImage(bi, this.getAttachImage());
			//绘制生命值
			bi = ImageUtils.addImage(bi, getHealthImage());
		}
		return bi;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getRace() {
		return race;
	}
	public void setRace(String race) {
		this.race = race;
	}
	public void setAttach(Integer attach) {
		this.attach = attach;
	}
	public void setHealth(Integer health) {
		this.health = health;
	}
	public BufferedImage getBg(){
		String bg = getCarTypeString() + "/bg.png";
		try {
			return ImageUtils.getImage(bg, ApiUtils.getHearthstoneImageApi(bg));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public BufferedImage getPicture(){
		try {
			BufferedImage picImage = ImageIO.read(ImageUtils.getRemoteInputStream(pic));
			Integer width = 400;
			Integer height = 400;
			//500X400
			if(height > picImage.getHeight() * (width*1.0/picImage.getWidth())){
				width = (int) (picImage.getWidth()*(height*1.0/picImage.getHeight()));
			}
			
			picImage =ImageUtils.scala(picImage, width);
			return picImage;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
